package com.tcs.test.poi;

import org.apache.poi.hslf.usermodel.HSLFSlideShow;
import org.apache.poi.sl.usermodel.Shape;
import org.apache.poi.sl.usermodel.Slide;
import org.apache.poi.sl.usermodel.SlideShow;
import org.apache.poi.xslf.usermodel.XMLSlideShow;
import org.junit.Test;

import java.io.*;
import java.util.List;

public class PPT {

    @SuppressWarnings({"rawtypes", "unchecked"})
    @Test
    public void testPpt() {
        String path = "D:\\TEMP\\ppt_demo\\第2课时  平均分（2）.ppt";
        // String path = "D:\\temp\\temp\\test.pptx";
        File file = new File(path);
        InputStream is = null;
        SlideShow slideShow = null;
        try {
            is = new FileInputStream(file);
            if (path.endsWith(".ppt")) {
                slideShow = new HSLFSlideShow(is);
            } else if (path.endsWith(".pptx")) {
                slideShow = new XMLSlideShow(is);
            }
            if (slideShow != null) {
                // 文本内容
                StringBuilder content = new StringBuilder();
                // 一页一页读取
                for (Slide slide : (List<Slide>) slideShow.getSlides()) {
                }
            }
        } catch (FileNotFoundException e) {
        } catch (IOException e) {
        } finally {
            try {
                if (slideShow != null) {
                    slideShow.close();
                }
                if (is != null) {
                    is.close();
                }
            } catch (IOException e) {
            }
        }
    }

    private Slide copySlide(Slide slide) throws IOException {
        Slide s1 = slide.getSlideShow().createSlide();
        List<Shape> shapes = slide.getShapes();
        for (Shape shape : shapes) {
            s1.addShape(shape);
        }
        return s1;
    }
}